// pages/classHomePage/group/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    group_task: null,
    people: null,
    title: null,
    card: [{
      title: {
        text: "分组",
        color: "#272635" //字体颜色
      },
      tag: {
        text: "1小时前"
      },
      header: {
        bgcolor: "#f5f5f5", //#f5f5f5  #fbde35
        line: true
      }
    }],
    funList: {
      'funitems': [{
          "name": "PK",
          "url": "/pages/classHomePage/pk/index/index",
          "pic_url": "/assets/wx_pic/menu_pk@3x.png"
        },
        {
          "name": "盲盒",
          "url": "/pages/classHomePage/blindbox/index/index",
          "pic_url": "/assets/wx_pic/menu_mh@3x.png"
        },
        {
          "name": "签到",
          "url": "/pages/classHomePage/signIn/index/index",
          "pic_url": "/assets/wx_pic/menu_qd@3x.png"
        },
        {
          "name": "辩论会",
          "url": "/pages/classHomePage/debate/index/index",
          "pic_url": "/assets/wx_pic/menu_blh@3x.png"
        },
        {
          "name": "抢答",
          "url": "/pages/classHomePage/qiangda/index/index",
          "pic_url": "/assets/wx_pic/menu_qd_1@3x.png"
        },
        {
          "name": "讨论",
          "url": "/pages/classHomePage/discuss/index/index",
          "pic_url": "/assets/wx_pic/menu_tl@3x.png"
        },
        {
          "name": "成员分组",
          "url": "/pages/classHomePage/group/index/index",
          "pic_url": "/assets/wx_pic/menu_zyfz@3x.png"
        },
        {
          "name": "积分",
          "url": "/pages/classHomePage/integral/index/index",
          "pic_url": "/assets/wx_pic/menu_jf@3x.png"
        },
      ]
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    this.setData({
      people: wx.getStorageSync("people")
    })
    _this.get_all_group_task().then(res => {
      console.log('进入页面加载：', res);
    })
  },

  inputTitle(e) {
    this.setData({
      title: e.detail.value
    })
  },

  formSubmit: function () {
    let _this = this
    // console.log(e);
    let title = _this.data.title
    if (title == '' || title == null) {
      console.log('不能为空');
      app.message('分组名称不能为空', 'error')
    } else {
      wx.showModal({
        title: '创建分组',
        editable: true, //显示输入框
        placeholderText: '输入分组人数', //显示输入框提示信息
        success: res => {
          if (res.confirm) { //点击了确认
            // console.log(res.content)//用户输入的值
            let data = {
              'code': wx.getStorageSync("myCode"),
              'course_name': app.globalData.banji_info['courseName'],
              'group_number': res.content,
              'group_type': title,
              'teacher_account': wx.getStorageSync("account"),
              'teacher_id': app.globalData.teacher_info['id']
            }
            // console.log(data);
            app.formPost('/create_group_task?', data).then(res => {
              console.log('正在分组：', res);
              if (res['code'] == 1) {
                console.log(res['message']);
                _this.get_all_group_task()
              }
            })
          } else {
            console.log('用户点击了取消')
          }
        }
      })
    }

  },

  gotoGroupPage: function (e) {
    console.log('click:', e);
    let groupId = e.currentTarget.dataset['groupid']
    wx.navigateTo({
      url: '/pages/classHomePage/group/groupPage/groupPage?groupId=' + groupId,
    })
  },

  get_all_group_task: function () {
    let _this = this
    let teacher_id = null
    let course_name = null
    if (wx.getStorageSync("people") == 'teacher') {
      teacher_id = app.globalData.teacher_info['id']
      course_name = app.globalData.banji_info['courseName']
    } else if (wx.getStorageSync("people") == 'student') {
      teacher_id = app.globalData.banji_info['teacher_id']
      course_name = app.globalData.banji_info['className']
    }
 
    return new Promise(function (resolve, reject) {
      let data = {
        'teacher_id': teacher_id,
        'course_name': course_name
      }
      app.formPost('/get_all_group_task?', data).then(res => {
        console.log('/get_all_group_task 获取老师所有的分组任务：', res);
        _this.setData({
          group_task: res
        })
        resolve(res)
      })
    })
  },

  click_item: function (e) {
    if (e.currentTarget.dataset.url != '') {
      wx.navigateBack()
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})